package com.shuda.business.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuda.business.crm.entity.FollowUpReminderEntity;
import com.shuda.business.crm.service.FollowUpReminderService;
import com.shuda.common.core.domain.R;
import com.shuda.common.core.domain.PageRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 跟进提醒Controller
 * @author shuda
 */
@RestController
@RequestMapping("/crm/follow-up-reminder")
@Tag(name = "跟进提醒管理", description = "跟进提醒相关接口")
@RequiredArgsConstructor
public class FollowUpReminderController {

    private final FollowUpReminderService followUpReminderService;

    /**
     * 分页查询跟进提醒
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询跟进提醒", description = "根据条件分页查询跟进提醒")
    public R<IPage<FollowUpReminderEntity>> getFollowUpReminderPage(
            @Parameter(description = "分页请求参数") PageRequest pageRequest,
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId,
            @Parameter(description = "客户ID") @RequestParam(required = false) Long customerId,
            @Parameter(description = "提醒状态") @RequestParam(required = false) String reminderStatus,
            @Parameter(description = "提醒方式") @RequestParam(required = false) String reminderType) {
        
        Page<FollowUpReminderEntity> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        IPage<FollowUpReminderEntity> result = followUpReminderService.getFollowUpReminderPage(page, userId, customerId, reminderStatus, reminderType);
        return R.ok(result);
    }

    /**
     * 根据客户ID获取跟进提醒
     */
    @GetMapping("/customer/{customerId}")
    @Operation(summary = "根据客户ID获取跟进提醒", description = "根据客户ID获取跟进提醒列表")
    public R<List<FollowUpReminderEntity>> getFollowUpRemindersByCustomerId(
            @Parameter(description = "客户ID") @PathVariable Long customerId,
            @Parameter(description = "提醒状态") @RequestParam(required = false) String reminderStatus) {
        
        List<FollowUpReminderEntity> result = followUpReminderService.getFollowUpRemindersByCustomerId(customerId, reminderStatus);
        return R.ok(result);
    }

    /**
     * 根据ID获取跟进提醒
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取跟进提醒", description = "根据ID获取跟进提醒详情")
    public R<FollowUpReminderEntity> getFollowUpReminderById(@Parameter(description = "提醒ID") @PathVariable Long id) {
        FollowUpReminderEntity reminderEntity = followUpReminderService.getFollowUpReminderById(id);
        return reminderEntity != null ? R.ok(reminderEntity) : R.fail("跟进提醒不存在");
    }

    /**
     * 创建跟进提醒
     */
    @PostMapping
    @Operation(summary = "创建跟进提醒", description = "创建跟进提醒")
    public R<Boolean> createFollowUpReminder(@RequestBody FollowUpReminderEntity reminderEntity) {
        boolean result = followUpReminderService.createFollowUpReminder(reminderEntity);
        return result ? R.ok(true) : R.fail("创建跟进提醒失败");
    }

    /**
     * 更新跟进提醒
     */
    @PutMapping
    @Operation(summary = "更新跟进提醒", description = "更新跟进提醒")
    public R<Boolean> updateFollowUpReminder(@RequestBody FollowUpReminderEntity reminderEntity) {
        boolean result = followUpReminderService.updateFollowUpReminder(reminderEntity);
        return result ? R.ok(true) : R.fail("更新跟进提醒失败");
    }

    /**
     * 删除跟进提醒
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除跟进提醒", description = "根据ID删除跟进提醒")
    public R<Boolean> deleteFollowUpReminder(@Parameter(description = "提醒ID") @PathVariable Long id) {
        boolean result = followUpReminderService.deleteFollowUpReminder(id);
        return result ? R.ok(true) : R.fail("删除跟进提醒失败");
    }

    /**
     * 批量删除跟进提醒
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除跟进提醒", description = "批量删除跟进提醒")
    public R<Boolean> batchDeleteFollowUpReminder(@RequestBody Long[] ids) {
        boolean result = followUpReminderService.batchDeleteFollowUpReminder(ids);
        return result ? R.ok(true) : R.fail("批量删除跟进提醒失败");
    }

    /**
     * 执行提醒
     */
    @PostMapping("/execute/{reminderId}")
    @Operation(summary = "执行提醒", description = "执行跟进提醒")
    public R<Boolean> executeReminder(@Parameter(description = "提醒ID") @PathVariable Long reminderId) {
        boolean result = followUpReminderService.executeReminder(reminderId);
        return result ? R.ok(true) : R.fail("执行提醒失败");
    }

    /**
     * 批量执行提醒
     */
    @PostMapping("/batch-execute")
    @Operation(summary = "批量执行提醒", description = "批量执行跟进提醒")
    public R<Boolean> batchExecuteReminder(@RequestBody Long[] reminderIds) {
        boolean result = followUpReminderService.batchExecuteReminder(reminderIds);
        return result ? R.ok(true) : R.fail("批量执行提醒失败");
    }

    /**
     * 取消提醒
     */
    @PostMapping("/cancel/{reminderId}")
    @Operation(summary = "取消提醒", description = "取消跟进提醒")
    public R<Boolean> cancelReminder(@Parameter(description = "提醒ID") @PathVariable Long reminderId) {
        boolean result = followUpReminderService.cancelReminder(reminderId);
        return result ? R.ok(true) : R.fail("取消提醒失败");
    }

    /**
     * 批量取消提醒
     */
    @PostMapping("/batch-cancel")
    @Operation(summary = "批量取消提醒", description = "批量取消跟进提醒")
    public R<Boolean> batchCancelReminder(@RequestBody Long[] reminderIds) {
        boolean result = followUpReminderService.batchCancelReminder(reminderIds);
        return result ? R.ok(true) : R.fail("批量取消提醒失败");
    }

    /**
     * 定时任务：处理待提醒的跟进提醒
     */
    @PostMapping("/process-pending")
    @Operation(summary = "处理待提醒的跟进提醒", description = "定时任务：处理待提醒的跟进提醒")
    public R<Integer> processPendingReminders() {
        int count = followUpReminderService.processPendingReminders();
        return R.ok(count);
    }
}